package com.jelly.web.modules.monitor.online.controller;

import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.note.OperationLog;
import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.base.domain.Base;
import com.jelly.web.modules.monitor.online.domain.OnLineUser;
import com.jelly.web.modules.monitor.online.service.IOnLineUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @创建人 ql
 * @创建时间 2018-09-15 14:27
 * @项目名称 jelly
 * @功能描述: 在线用户控制器
 **/
@Controller
@RequestMapping("/admin/monitor/online")
public class OnLineUserController extends BaseController {

    @Autowired
    private IOnLineUserService onLineUserService;

    /**
     * 在线用户列表跳转
     * @return
     */
    @GetMapping
    public String getOnLinUser(){

        //获取字典详情
        getDictionary();
        //获取该用户可访问的按钮详情
        getButton();

        return adminOnLineUser("/list");
    }


    /**
     * 获取在线用户列表
     * @return
     */
    @PostMapping
    @ResponseBody
    public Map<String,Object> getOnLinUsers(OnLineUser onLineUser){

        Map<String,Object> maps = new HashMap<>();
        List<OnLineUser> list = onLineUserService.getOnLineUserAll(onLineUser);
        maps.put("code",0);
        maps.put("count",list.size());
        maps.put("data",list);

        return maps;

    }

    /**
     * 强制下线
     * @param base
     * @return
     */
    @PostMapping("/delect")
    @OperationLog(name = "在线用户模块",value = GlobalConstant.DELECT)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectDept(Base base){

        if(StringUtils.isNotBlank(base.getBaseId())){
            if(base.getBaseId().equals(TokenUtil.getSession().getId())){
                return errorResponse("您不能强制下线自己，请稍后重试");
            }
            onLineUserService.delectOnLineUser(base.getBaseId());
            return okResponse("强制下线成功");
        }

        return errorResponse("强制下线失败");

    }

    /**
     * 批量强制下线
     * @param base
     * @return
     */
    @PostMapping("/delects")
    @OperationLog(name = "在线用户模块",value = GlobalConstant.DELECTS)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectsDept(Base base){

        if(ListUtil.isNotList(base.getIds())){
            if(base.getIds().contains(TokenUtil.getSession().getId().toString())){
                return errorResponse("您不能强制下线自己，请稍后重试");
            }
            onLineUserService.delectOnLineUser(base.getIds());
            return okResponse("强制下线成功");
        }

        return errorResponse("强制下线失败");

    }

}
